package com.greenline.search.report;

import com.greenline.search.report.entity.LogReportResult;
import com.greenline.search.report.entity.SearchWord;
import com.greenline.search.report.model.Configuration;
import com.greenline.search.report.model.GenerateReportResult;
import com.greenline.search.report.model.GetNoResultsWord;
import com.greenline.search.report.util.*;
import com.greenline.search.test.utils.JSONStreamParserForAnalyze;
import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 14-2-12
 * Time: 下午2:39
 * To change this template use File | Settings | File Templates.
 */
public class TopNoResultsWordEmail {
    private static Logger logger = LoggerFactory.getLogger(TopNoResultsWordEmail.class);
    //上周日志文件名称列表（不要被名字所迷惑）
    private List<String> currentWeekLogNameList;
    //上周搜索词列表
    private List<SearchWord> currentWeekSearchWordList;

    //初始化搜索词列表
    public TopNoResultsWordEmail(){
        Date today = new Date();
        currentWeekLogNameList = Arrays.asList(DateUtils.getLastWeekLogNameArray(today,Configuration.WEEK_DAY_COUNT));
        currentWeekSearchWordList = new LinkedList<SearchWord>();
        SearchWordListUtil.initSearchWordList(currentWeekLogNameList,currentWeekSearchWordList);
        Collections.sort(currentWeekSearchWordList);
        for(int i = 0; i < currentWeekSearchWordList.size(); i++){
            SearchWord searchWord = currentWeekSearchWordList.get(i);
            searchWord.setCurrentWeekRank(i+1);
            currentWeekSearchWordList.set(i,searchWord);
        }
    }


    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();

        String[] currentLognameArray = DateUtils.getLastWeekLogNameArray(new Date(),Configuration.WEEK_DAY_COUNT);
        String timeLine = "("+currentLognameArray[0]+"-"+currentLognameArray[currentLognameArray.length-1]+")";
        System.out.println(Configuration.OBJECT_PATH);
        logger.info("Get no result word...");
        String fileName = "no_result_word_"+timeLine;
        System.out.println(Configuration.OBJECT_PATH);
        GetNoResultsWord noResultsWord = (GetNoResultsWord) ObjectSerializationUtil.readReportResult(fileName);
        if(noResultsWord == null){
            TopNoResultsWordEmail searchLogReport = new TopNoResultsWordEmail();
            noResultsWord = new GetNoResultsWord(searchLogReport.getCurrentWeekSearchWordList());
            ObjectSerializationUtil.writeReportResult(fileName,noResultsWord);
        }

        String content = FormatNoResultWordUtil.getAllFormatedResult(
                noResultsWord.getDoctorNoResultsWord(),noResultsWord.getHospitalNoResultsWord());
        EmailUtils.sendEmail("搜索无结果词周报"+timeLine, content);

        logger.info("Total cost time {} seconds.", (System.currentTimeMillis()-startTime)/1000);
    }

    public List<SearchWord> getCurrentWeekSearchWordList() {
        return currentWeekSearchWordList;
    }
}